﻿using System;
using VictorBlaga.DocumentManagement.Core.Models;
using VictorBlaga.DocumentManagement.Core.Models.Repositories;
using VictorBlaga.DocumentManagement.Models;

namespace VictorBlaga.DocumentManagement.Helpers.Mappers.Impl
{
    public class TeamMapper : ITeamMapper
    {
        private readonly IUserRepository _userRepository;

        public TeamMapper(IUserRepository userRepository)
        {
            _userRepository = userRepository;
        }

        public TeamViewModel MapToViewModel(Team model)
        {
            var viewModel = new TeamViewModel();
            if (model != null)
            {
                viewModel.Id = model.ID;
                viewModel.Name = model.Name;
                if(model.Manager!=null)
                {
                    viewModel.ManagerId = model.Manager.ID;
                    viewModel.ManagerName = model.Manager.Fullname;
                }
                else
                {
                    viewModel.ManagerId = null;
                    viewModel.ManagerName = string.Empty;
                }
            }
            return viewModel;
        }

        public void MapToModel(TeamViewModel viewModel, Team model)
        {
            model.Name = viewModel.Name;
            model.Manager = _userRepository.GetByID((int) viewModel.ManagerId);
        }
    }
}